﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using DAL;
using MySql.Data.MySqlClient;
using System.Threading;
using System.Windows.Forms.DataVisualization.Charting;
using Liju.Common;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using LiJu.Common;

namespace BLL
{
    /// <summary>
    /// 数据库业务
    /// </summary>
    public class Bll
    {
        /// <summary>
        /// 查 COM端口设置列表
        /// </summary>
        /// <returns></returns>
        public static DataTable GetComPortList()
        {
            string strMySql = "select * from ComPort";
            DataTable dt = DalMySQL.ExecuteDataSet(strMySql);
            return dt;
        }

        /// <summary>
        /// 更新COM端口设置 0力臂 1电批 2套筒 3上料机
        /// </summary>
        /// <param name="PortType">0力臂 1电批 2套筒 3上料机</param>
        /// <param name="PortName">COM端口名称</param>
        /// <returns></returns>
        public static int UpdateComPort(int PortType, string PortName)
        {
            string strMySql = "update ComPort set PortName='" + PortName + "' where PortType=" + PortType;
            return DalMySQL.ExecuteNonQuery(strMySql);

            //执行不报错 但不成功 原因待查
            //string strMySql = "update ComPort set PortName='@PortName' where PortType=@PortType";
            //MySqlParameter[] parms = new MySqlParameter[]{
            //    new MySqlParameter("@PortName", PortName),
            //    new MySqlParameter("@PortType", PortType)
            //};
            //return DalMySQL.ExecuteNonQuery(strMySql, parms);
        }



        #region //2024.05.09

        /// <summary>
        /// 查询用户是否存在
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public static DataTable QueryLoginUser(string UserId, string Password)
        {
            string strMySql = "select * from usermanager where UserId='" + UserId + "' And Password='" + Password + "'";
            DataTable dt = DalMySQL.ExecuteDataSet(strMySql);
            return dt;
        }


        /// <summary>
        /// 获取参数表
        /// </summary>
        /// <returns></returns>
        public static DataTable GetParameterData()
        {
            string strMySql = "select Content,Alias from parameter";
            DataTable dt = DalMySQL.ExecuteDataSet(strMySql);
            return dt;
        }

        /// <summary>
        /// 获取步骤表
        /// </summary>
        /// <returns></returns>
        public static DataTable GetStepData()
        {
            string strMySql = "select step,lap,speed,MiniTorqueLength,MaxTorqueLength,SafeTorqueLength,TargetTorqueLength,MiniAngle,MaxAngle,SafeAngle,TargetAngle from step";
            DataTable dt = DalMySQL.ExecuteDataSet(strMySql);
            return dt;
        }

        /// <summary>
        /// 获取步骤明细表
        /// </summary>
        /// <returns></returns>
        public static DataTable GetStepDetailData()
        {
            string strMySql = "select * from stepdetail";
            DataTable dt = DalMySQL.ExecuteDataSet(strMySql);
            return dt;
        }


        /// <summary>
        /// 获取设置表格数据
        /// </summary>
        /// <returns></returns>
        public static DataTable GetSettingData()
        {
            string strMySql = "select * from setting";
            DataTable dt = DalMySQL.ExecuteDataSet(strMySql);
            return dt;

        }

        /// <summary>
        /// 获取用户管理数据
        /// </summary>
        /// <returns></returns>
        public static DataTable GetUserManagerData()
        {
            string strMySql = "select Id,UserId,UserName,UserRole,State from usermanager";
            DataTable dt = DalMySQL.ExecuteDataSet(strMySql);
            return dt;

        }

        /// <summary>
        /// 获取角色管理数据
        /// </summary>
        /// <returns></returns>
        public static DataTable GetRoleManagerData()
        {
            string strMySql = "select * from RoleManager";
            DataTable dt = DalMySQL.ExecuteDataSet(strMySql);
            return dt;

        }

        /// <summary>
        /// 获取参数表
        /// </summary>
        /// <returns></returns>
        public static DataTable GetTCPIDData()
        {
            string strMySql = "select ComPortID from comport where PortType='1'";
            DataTable dt = DalMySQL.ExecuteDataSet(strMySql);
            return dt;
        }

        /// <summary>
        /// 获取参数表
        /// </summary>
        /// <returns></returns>
        public static DataTable GetTCPData()
        {
            string strMySql = "select * from comport";
            DataTable dt = DalMySQL.ExecuteDataSet(strMySql);
            return dt;
        }

        /// <summary>
        /// 更新设备参数数据 
        /// </summary>
        /// <param name="ComPortID">ID</param>
        /// <param name="PortName">COM</param>
        /// <param name="BaudRate">波特率</param>
        /// <param name="Smac">锁MAC</param>
        /// <returns></returns>
        public static int UpdateComPortDaTa(int ComPortID, string PortName, string Smac = "")
        {

            string sqls = $"UPDATE comport SET PortName='{PortName}',Smac='{Smac}' WHERE ComPortID={ComPortID}";


            if (String.IsNullOrWhiteSpace(PortName))
            {
                sqls = $"UPDATE comport SET Smac='{Smac}' WHERE ComPortID={ComPortID}";
            }

            return DalMySQL.ExecuteNonQuery(sqls);
        }

        /// <summary>
        /// 添加异常报修记录
        /// </summary>
        /// <param name="toolCode">工器具编码</param>
        /// <param name="toolType">工器具类型</param>
        /// <param name="work">工位</param>
        /// <param name="line">产线</param>
        /// <param name="errType">异常大类</param>
        /// <param name="errDateil">异常明细</param>
        /// <param name="errRemark">其它异常</param>
        /// <returns></returns>
        public static int InsertErrRecordData(string toolCode, string toolType, string work, string line, string errType, string errDateil, string errRemark)
        {
            string sqls = $"INSERT INTO errrecord VALUES(NULL,'{toolCode}','{toolType}','{work}','{line}','{errType}','{errDateil}','{errRemark}','{DateTime.Now.ToString()}')";

            return DalMySQL.ExecuteNonQuery(sqls);

        }



        /// <summary>
        /// 添加异常报修记录
        /// </summary>
        /// <param name="toolCode">工器具编码</param>
        /// <param name="toolType">工器具类型</param>
        /// <param name="work">工位</param>
        /// <param name="line">产线</param>
        /// <param name="errType">异常大类</param>
        /// <param name="errDateil">异常明细</param>
        /// <param name="errRemark">其它异常</param>
        /// <returns></returns>
        public static int InsertProductInspectionData(string barcode, string ordername, string orderstartdate, int orderqty, string productcode, string productname,
            string curstation, string reworkflow, string reworkstep, string workflowstepid, string reason, string remark)
        {

            string sqls = $"INSERT INTO productinspection (barcode,mfgordername,mfgorderstartdate,mfgorderqty,productcode,productname,curstation,reworkflow,reworkstep,workflowstepid,reason,remark,createtime) " +
                $"VALUES('{barcode}','{ordername}','{orderstartdate}',{orderqty},'{productcode}','{productname}','{curstation}','{reworkflow}','{reworkstep}','{workflowstepid}','{reason}','{remark}','{DateTime.Now.ToString()}')";

            return DalMySQL.ExecuteNonQuery(sqls);
        }


        /// <summary>
        /// 新增参数表
        /// </summary>
        /// <param name="PortType"></param>
        /// <param name="PortName"></param>
        /// <returns></returns>
        public static int InsertParameterData(string content)
        {
            string strMySql = "INSERT INTO Parameter VALUES (NULL,'" + content + "');";
            return DalMySQL.ExecuteNonQuery(strMySql);

        }

        /// <summary>
        /// 新增步骤明细表
        /// </summary>
        /// <param name="PortType"></param>
        /// <param name="PortName"></param>
        /// <returns></returns>
        public static int InsertStepDetailData(string Name, string Parameter, string Round, string ParameterUnit, string Jump, string Remark)
        {
            string strMySql = "INSERT INTO StepDetail VALUES (NULL,'" + Name + "','" + Parameter + "', '" + Round + "','" + ParameterUnit + "','" + Jump + "','" + Remark + "');";
            return DalMySQL.ExecuteNonQuery(strMySql);

        }

        /// <summary>
        /// 新增步骤表
        /// </summary>
        /// <param name="PortType"></param>
        /// <param name="PortName"></param>
        /// <returns></returns>
        public static int InsertStepData(string TaskId, string Step, string Lap, string Speed, string MiniTorqueLength, string MaxTorqueLength, string SafeTorqueLength, string Anglethreshold, string TargetTorqueLength, string MiniAngle, string MaxAngle, string SafeAngle, string TargetAngle, string StepType, string TighteningType)
        {
            string strMySql = "INSERT INTO Step VALUES (null,'" + TaskId + "','" + Step + "','" + Lap + "','" + Speed + "','" + MiniTorqueLength + "','" + MaxTorqueLength + "','" + SafeTorqueLength + "','" + Anglethreshold + "','" + TargetTorqueLength + "','" + MiniAngle + "','" + MaxAngle + "','" + SafeAngle + "','" + TargetAngle + "','" + StepType + "','" + TighteningType + "')";
            return DalMySQL.ExecuteNonQuery(strMySql);

        }


        /// <summary>
        /// 新增/修改 - 设置
        /// </summary>
        /// <param name="Id" ></param>
        /// <param name="SaveRecordDay">记录保存天数</param>
        /// <param name="CoordinateDeviation">坐标偏差</param>
        /// <returns></returns>
        public static int InsertAndUpdateSettingData(int Id, string SaveRecordDay, string CoordinateDeviation, string CoordinateDeviationZ,
            int DianPiSpeedMax, bool OKDianPiEnable, bool NGIsFinisth, int GongXuNo, string TaoTongStateList, int TuiGanRunTime,
            int LedEnable, string MesIP, string MesPort, string MesLine, string MesWorkStation, string MesZiYuanNo, int WeiYIChaoXianTime)
        {
            if (Id > 0)
            {
                string strMySql = string.Format(@"UPDATE setting set SaveRecordDay='{0}',CoordinateDeviation='{1}',
                    CoordinateDeviationZ='{2}', DianPiSpeedMax={3}, OKDianPiEnable={4}, NGIsFinisth={5}, 
                    GongXuNo={6}, TaoTongStateList='{7}', TuiGanRunTime={8}, LedEnable={9}, MesIP='{10}', MesPort='{11}', 
                    MesLine='{12}', MesWorkStation='{13}', MesZiYuanNo='{14}',WeiYIChaoXianTime={15} where Id={16}", SaveRecordDay,
                    CoordinateDeviation, CoordinateDeviationZ, DianPiSpeedMax, OKDianPiEnable, NGIsFinisth, GongXuNo,
                    TaoTongStateList, TuiGanRunTime, LedEnable, MesIP, MesPort, MesLine, MesWorkStation, MesZiYuanNo,
                    WeiYIChaoXianTime, Id);
                return DalMySQL.ExecuteNonQuery(strMySql);
            }
            else
            {
                string strMySql = string.Format(@"INSERT INTO setting (SaveRecordDay, CoordinateDeviation, CoordinateDeviationZ, 
                    DianPiSpeedMax, OKDianPiEnable, NGIsFinisth, GongXuNo, TaoTongStateList, TuiGanRunTime, LedEnable ) VALUES('{0}',
                    '{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}')", SaveRecordDay,
                    CoordinateDeviation, CoordinateDeviationZ, DianPiSpeedMax, OKDianPiEnable, NGIsFinisth, GongXuNo,
                    TaoTongStateList, TuiGanRunTime, LedEnable, MesIP, MesPort, MesLine, MesWorkStation, MesZiYuanNo);
                return DalMySQL.ExecuteNonQuery(strMySql);
            }

        }

        /// <summary>
        /// 更新MES设置
        /// </summary>
        /// <param name="MesIP"></param>
        /// <param name="MesPort"></param>
        /// <param name="MesLine"></param>
        /// <param name="MesWorkStation"></param>
        /// <param name="MesZiYuanNo"></param>
        /// <returns></returns>
        public static int UpdateMesSet(string MesIP, string MesPort, string MesLine, string MesWorkStation, string MesZiYuanNo, string GongXuNum)
        {
            string strMySql = "UPDATE setting set MesIP='" + MesIP + "', MesPort='" + MesPort + "', MesLine='" + MesLine +
                "', MesWorkStation='" + MesWorkStation + "', MesZiYuanNo='" + MesZiYuanNo + "',GongXuNo='" + GongXuNum + "';";
            return DalMySQL.ExecuteNonQuery(strMySql);
        }

        /// <summary>
        /// 添加用户管理数据
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="UserName"></param>
        /// <param name="UserRole"></param>
        /// <param name="State"></param>
        /// <param name="Operate"></param>
        /// <returns></returns>
        public static int InsertUserManagerData(string UserId, string UserName, string Password, string UserRole, string State)
        {
            string strMySql = "INSERT INTO usermanager VALUES (NULL,'" + UserId + "','" + UserName + "','" + Password + "','" + UserRole + "','" + State + "');";
            return DalMySQL.ExecuteNonQuery(strMySql);

        }

        /// <summary>
        /// 新增参数表
        /// </summary>
        /// <param name="PortType"></param>
        /// <param name="PortName"></param>
        /// <returns></returns>
        public static int InsertTCPData(string porttype, string ipaddress, string port)
        {
            string strMySql = "INSERT INTO comport VALUES (NULL,'" + porttype + "','" + ipaddress + "','" + port + "');";
            return DalMySQL.ExecuteNonQuery(strMySql);

        }

        /// <summary>
        /// 更新参数表
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static int UpdateParameterData(int Id, string content)
        {
            string strMySql = "update Parameter set content='" + content + "' where ID=" + Id;
            return DalMySQL.ExecuteNonQuery(strMySql);
        }

        /// <summary>
        /// 更新参数表
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static int UpdateParameterAliasData(int TaskId, string alias)
        {
            string strMySql = "update Parameter set Alias='" + alias + "' where TaskID=" + TaskId;
            return DalMySQL.ExecuteNonQuery(strMySql);
        }

        /// <summary>
        /// 更新步骤明细表
        /// </summary>
        /// <param name="Id">id</param>
        /// <param name="Name">名称</param>
        /// <param name="Parameter">参数</param>
        /// <param name="Round">范围</param>
        /// <param name="ParameterUnit">单位</param>
        /// <param name="Forbidden">跳过</param>
        /// <param name="Remark">备注</param>
        /// <returns></returns>
        public static int UpdateStepDetailData(int Id, string Name, string Parameter, string Round, string ParameterUnit, string Forbidden, string Remark)
        {
            string strMySql = "UPDATE StepDetail SET Name = '" + Name + "', Parameter = '" + Parameter + "',Round = '" + Round + "',ParameterUnit = '" + ParameterUnit + "',Forbidden = '" + Forbidden + "',Remark = '" + Remark + "' WHERE id = " + Id;
            return DalMySQL.ExecuteNonQuery(strMySql);
        }

        /// <summary>
        /// 更新步骤表
        /// </summary>
        /// <param name="ID">id</param>
        /// <param name="Step">步骤</param>
        /// <param name="Lap">圈数</param>
        /// <param name="Speed">速度</param>
        /// <param name="MiniTorqueLength">最小力矩</param>
        /// <param name="MaxTorqueLength">最大力矩</param>
        /// <param name="SafeTorqueLength">安全力矩</param>
        /// <param name="Anglethreshold">角度阈值</param>
        /// <param name="TargetTorqueLength">目标力矩</param>
        /// <param name="MiniAngle">最小角度</param>
        /// <param name="MaxAngle">最大角度</param>
        /// <param name="SafeAngle">安全角度</param>
        /// <param name="TargetAngle">目标角度</param>
        /// <returns></returns>
        public static int UpdateStepData(int ID, string Step, string Lap, string Speed, string MiniTorqueLength, string MaxTorqueLength, string SafeTorqueLength, string Anglethreshold, string TargetTorqueLength, string MiniAngle, string MaxAngle, string SafeAngle, string TargetAngle)
        {


            string mySqls = string.Format($@"UPDATE step SET Step='{Step}',Lap='{Lap}',Speed='{Speed}',MiniTorqueLength='{MiniTorqueLength}',MaxTorqueLength='{MaxTorqueLength}',
                                SafeTorqueLength='{SafeTorqueLength}',Anglethreshold='{Anglethreshold}',TargetTorqueLength='{TargetTorqueLength}',
                                MiniAngle='{MiniAngle}',MaxAngle='{MaxAngle}',SafeAngle='{SafeAngle}',TargetAngle='{TargetAngle}' WHERE ID={ID}");


            return DalMySQL.ExecuteNonQuery(mySqls);
        }


        /// <summary>
        /// 更新用户管理数据
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="UserId"></param>
        /// <param name="UserName"></param>
        /// <param name="UserRole"></param>
        /// <param name="State"></param>
        /// <param name="Operate"></param>
        /// <returns></returns>
        public static int UpdateUserManagerData(string Id, string UserId, string UserName, string Password, string UserRole, string State)
        {
            string strMySql = "UPDATE usermanager SET UserId = '" + UserId + "', UserName = '" + UserName + "', Password = '" + Password + "',UserRole='" + UserRole + "', State = '" + State + "' WHERE id = " + Id;
            return DalMySQL.ExecuteNonQuery(strMySql);
        }

        /// <summary>
        /// 更新角色管理
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="RoleName"></param>
        /// <param name="RolePermissions"></param>
        /// <param name="EmployCount"></param>
        /// <param name="Operate"></param>
        /// <returns></returns>
        public static int UpdateRoleManagerData(string Id, string RoleName, string RolePermissions, string EmployCount, string Operate)
        {
            string strMySql = "UPDATE usermanager SET RoleName = '" + RoleName + "', RolePermissions = '" + RolePermissions + "', EmployCount = '" + EmployCount + "',Operate='" + Operate + "' WHERE id = " + Id;
            return DalMySQL.ExecuteNonQuery(strMySql);
        }

        /// <summary>
        /// 更新IP地址表
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static int UpdateTCPData(int Id, string ipaddress, string port)
        {
            string strMySql = "update comport set PortType='" + 1 + "', PortName='" + ipaddress + "',BaudRate='" + port + "' where ComPortID=" + Id;
            return DalMySQL.ExecuteNonQuery(strMySql);
        }



        /// <summary>
        /// 删除参数表
        /// </summary>
        /// <param name="Id">id</param>
        /// <returns></returns>
        public static int DeleteParameterData(string Id)
        {
            string strMySql = "Delete FROM Parameter where ID=" + Id;
            return DalMySQL.ExecuteNonQuery(strMySql);
        }

        /// <summary>
        /// 删除参数表
        /// </summary>
        /// <param name="Id">id</param>
        /// <returns></returns>
        public static int DeleteParameterWithTaskIDData(string TaskId)
        {
            string strMySql = "Delete FROM Parameter where TaskID=" + TaskId;
            return DalMySQL.ExecuteNonQuery(strMySql);
        }


        /// <summary>
        /// 删除步骤明细表
        /// </summary>
        /// <param name="Id">id</param>
        /// <returns></returns>
        public static int DeleteStepDetailData(string Id)
        {
            string strMySql = "Delete FROM StepDetail  WHERE id = " + Id;
            return DalMySQL.ExecuteNonQuery(strMySql);
        }

        /// <summary>
        /// 删除步骤表
        /// </summary>
        /// <param name="Id">id</param>
        /// <returns></returns>
        public static int DeleteStepData(string Id)
        {
            string strMySql = "Delete FROM Step  WHERE id = " + Id;
            return DalMySQL.ExecuteNonQuery(strMySql);
        }

        /// <summary>
        /// 删除步骤表
        /// </summary>
        /// <param name="Id">id</param>
        /// <returns></returns>
        public static int DeleteStepWithTaskIDData(string TaskId)
        {
            string strMySql = "Delete FROM Step  WHERE TaskID = " + TaskId;
            return DalMySQL.ExecuteNonQuery(strMySql);
        }

        /// <summary>
        /// 删除用户管理数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static int DeleteUserManagerData(string Id)
        {
            string strMySql = "Delete FROM usermanager  WHERE id = " + Id;
            return DalMySQL.ExecuteNonQuery(strMySql);
        }

        /// <summary>
        /// 删除角色管理
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static int DeleteRoleManagerData(string Id)
        {
            string strMySql = "Delete FROM rolemanager  WHERE id = " + Id;
            return DalMySQL.ExecuteNonQuery(strMySql);
        }

        #endregion


        #region 20240524

        /// <summary>
        /// 根据任务号查询步骤
        /// </summary>
        /// <param name="tasknumber"></param>
        /// <returns></returns>
        public static DataTable GetTaskWithStep(int tasknumber)
        {
            string strMySql = "select * from step where TaskID='" + tasknumber + "'";
            DataTable dt = DalMySQL.ExecuteDataSet(strMySql);
            return dt;
        }

        /// <summary>
        /// 获取步骤表
        /// </summary>
        /// <returns></returns>
        public static DataTable QueryTaskIdWithStepData(string tasknumber)
        {
            string strMySql = "select ID, step,lap,speed,MiniTorqueLength,MaxTorqueLength,SafeTorqueLength,Anglethreshold,TargetTorqueLength,MiniAngle,MaxAngle,SafeAngle,TargetAngle,StepType,TighteningType from step where TaskID='" + tasknumber + "'";
            DataTable dt = DalMySQL.ExecuteDataSet(strMySql);
            return dt;
        }

        public static DataTable QueryContentWithTaskID(string Content)
        {
            string strMySql = "select * from parameter where Content='" + Content + "'";
            DataTable dt = DalMySQL.ExecuteDataSet(strMySql);
            return dt;
        }

        /// <summary>
        /// 新增参数表
        /// </summary>
        /// <param name="PortType"></param>
        /// <param name="PortName"></param>
        /// <returns></returns>
        public static int InsertParameterData(string content, string taskId, string notes)
        {
            string strMySql = "INSERT INTO Parameter VALUES (NULL,'" + content + "','" + taskId + "','" + notes + "');";
            return DalMySQL.ExecuteNonQuery(strMySql);

        }

        #endregion

        #region 20240527

        public static DataTable GetDevice()
        {
            string strMySql = "select Screw,InventoryLowerLimit,InventoryCeiling,CurrentInventory,ReplenishmentQuantity,Increase,Reduce from screwcompartment";
            DataTable dt = DalMySQL.ExecuteDataSet(strMySql);
            return dt;
        }

        public static int UpdateScrewCompartmentData(int Id, string InventoryLowerLimit, string InventoryCeiling, string ReplenishmentQuantity, string Increase, string Reduce)
        {
            string strMySql = "update screwcompartment set InventoryLowerLimit='" + InventoryLowerLimit + "', InventoryCeiling='" + InventoryCeiling + "',ReplenishmentQuantity='" + ReplenishmentQuantity + "',Increase='" + Increase + "',Reduce='" + Reduce + "' where Id=" + Id;
            return DalMySQL.ExecuteNonQuery(strMySql);
        }

        public static DataTable GetFullDevice()
        {
            string strMySql = "select * from screwcompartment";
            DataTable dt = DalMySQL.ExecuteDataSet(strMySql);
            return dt;
        }

        #endregion


        #region 20240530

        public static DataTable QueryStepByTaskIdWithStep(string Step, string TaskId)
        {
            string strMySql = "select * from step where Step='" + Step + "' AND TaskId='" + TaskId + "'";
            DataTable dt = DalMySQL.ExecuteDataSet(strMySql);
            return dt;
        }

        /// <summary>
        /// 删除步骤表
        /// </summary>
        /// <param name="Id">id</param>
        /// <returns></returns>
        public static int DeleteStepByTaskIdData(string StepId, string TaskId)
        {
            string strMySql = "Delete FROM Step  where Step=" + StepId + " AND TaskID=" + TaskId + "";
            return DalMySQL.ExecuteNonQuery(strMySql);
        }

        public static DataTable GetFullDevices()
        {
            string strMySql = "select device,online,operate,port from communicate";
            DataTable dt = DalMySQL.ExecuteDataSet(strMySql);
            return dt;
        }

        public static int UpdateCommunicateOnlineData(string device, string online)
        {
            string strMySql = "update communicate set online='" + online + "' where device='" + device + "'";
            return DalMySQL.ExecuteNonQuery(strMySql);
        }

        #endregion

        #region

        public static void TruncateParameterAndStep()
        {

            //string strMySql3 = "delete from step";
            //DalMySQL.ExecuteNonQuery(strMySql3);
            //Thread.Sleep(50);
            //string strMySql4 = "delete from parameter";
            //DalMySQL.ExecuteNonQuery(strMySql4);
            //Thread.Sleep(50);

            //string strMySql1 = "alter table step AUTO_INCREMENT=0";
            //DalMySQL.ExecuteNonQuery(strMySql1);
            //Thread.Sleep(50);
            //string strMySql = "alter table parameter AUTO_INCREMENT=0";
            //DalMySQL.ExecuteNonQuery(strMySql);

            string strMySql1 = "truncate table step";
            DalMySQL.ExecuteNonQuery(strMySql1);
            string strMySql = "truncate table parameter";
            DalMySQL.ExecuteNonQuery(strMySql);

        }

        #endregion

        #region

        public static DataTable GetBatchMaxSpeed()
        {
            string strMySql = "select DianPiSpeedMax from setting";
            DataTable dt = DalMySQL.ExecuteDataSet(strMySql);
            return dt;
        }

        #endregion

        /// <summary>
        /// 获取参数表的任务内容
        /// </summary>
        /// <returns></returns>
        public static DataTable GetParameterTaskID()
        {
            string strMySql = "select TaskID from parameter";
            DataTable dt = DalMySQL.ExecuteDataSet(strMySql);
            return dt;
        }

        #region 2024.8.6 hang
        /// <summary>
        /// 添加DDK参数
        /// </summary>
        /// <param name="screwParam"></param>
        /// <returns></returns>
        public static int AddDDKParameterData(ScrewParam screwParam)
        {
            string strMySql = $"INSERT INTO ddkparameter VALUES ({screwParam.taskId},{screwParam.taskId},'{screwParam.name}','{screwParam.tighteningType}',{screwParam.targetTorque},{screwParam.targeAngle},{screwParam.allRangeTorque},{screwParam.maxTorqueLowLimit},{screwParam.maxTorqueUpLimit},{screwParam.slowTorque},{screwParam.firstTorque},{screwParam.sungTorque},{screwParam.secendTorque},{screwParam.lastTorqueLowlimit},{screwParam.lastTorqueUplimit},{screwParam.firstAngle},{screwParam.secendAngle},{screwParam.startTime},{screwParam.startSpeed},{screwParam.noTorqueSpeed},{screwParam.firstSpeed},{screwParam.secendSpeed},{screwParam.thirdSpeed},{screwParam.reverseSpeed},{screwParam.firstTimeUpLimit},{screwParam.secendTimeUpLimit},{screwParam.lastAngleLowlimit},{screwParam.lastAngleUplimit});";
            return DalMySQL.ExecuteNonQuery(strMySql);

        }

        /// <summary>
        /// 查询所有DDK参数
        /// </summary>
        /// <returns></returns>
        public static List<ScrewParam> GetAllDDKParameter()
        {
            //获取没有删除的产品
            string strMySql = $"select * from ddkparameter order by taskId";
            DataTable dt = DalMySQL.ExecuteDataSet(strMySql);
            var parameters = LiJu.DbExtension.DataTableToList<ScrewParam>(dt);
            return parameters;
        }
        /// <summary>
        /// 清除DDK参数表
        /// </summary>
        /// <param name="Id">id</param>
        /// <returns></returns>
        public static int DeleteAllDDKParameter()
        {
            string strMySql = "delete  FROM ddkparameter";
            return DalMySQL.ExecuteNonQuery(strMySql);
        }
        /// <summary>
        /// 根据TaskID删除DDK参数表
        /// </summary>
        /// <param name="Id">id</param>
        /// <returns></returns>
        public static int DeleteDDKParameterByTaskID(string TaskId)
        {
            string strMySql = "Delete FROM ddkparameter where TaskID=" + TaskId;
            return DalMySQL.ExecuteNonQuery(strMySql);
        }
        #endregion
        /// <summary>
        ///  add zhl 修改DDK参数
        /// </summary>
        /// <param name="screwParam"></param>
        /// <returns></returns>
        public static int UpdateDDKParameterData(ScrewParam screwParam)
        {
            string strMySql = string.Format(@"update ddkparameter set tighteningType='{0}',targeAngle={1},
                    lastAngleLowlimit={2}, lastAngleUplimit={3}, targetTorque={4}, maxTorqueLowLimit={5}, 
                    maxTorqueUpLimit={6}, firstTorque={7}, sungTorque={8}, secendTorque={9}, firstTimeUpLimit={10}, secendTimeUpLimit={11}, 
                    firstSpeed={12}, secendSpeed={13},name='{14}' where taskId={15}", screwParam.tighteningType,
    screwParam.targeAngle, screwParam.lastAngleLowlimit, screwParam.lastAngleUplimit, screwParam.targetTorque, screwParam.maxTorqueLowLimit, screwParam.maxTorqueUpLimit,
    screwParam.firstTorque, screwParam.sungTorque, screwParam.secendTorque, screwParam.firstTimeUpLimit, screwParam.secendTimeUpLimit, screwParam.firstSpeed, screwParam.secendSpeed,
    screwParam.name, screwParam.taskId);
            return DalMySQL.ExecuteNonQuery(strMySql);
        }
    }
}
